package org.dromara.bzIns.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.bzIns.domain.vo.NoticeVo;
import org.dromara.bzIns.domain.bo.NoticeBo;
import org.dromara.bzIns.service.INoticeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 通知信息
 *
 * @author bz_huaYal9146
 * @date 2025-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bzIns/notice")
public class NoticeController extends BaseController {

    private final INoticeService noticeService;

    /**
     * 查询通知信息列表
     */
    @SaCheckPermission("bzIns:notice:list")
    @GetMapping("/list")
    public TableDataInfo<NoticeVo> list(NoticeBo bo, PageQuery pageQuery) {
        return noticeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出通知信息列表
     */
    @SaCheckPermission("bzIns:notice:export")
    @Log(title = "通知信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NoticeBo bo, HttpServletResponse response) {
        List<NoticeVo> list = noticeService.queryList(bo);
        ExcelUtil.exportExcel(list, "通知信息", NoticeVo.class, response);
    }

    /**
     * 获取通知信息详细信息
     *
     * @param noticeId 主键
     */
    @SaCheckPermission("bzIns:notice:query")
    @GetMapping("/{noticeId}")
    public R<NoticeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long noticeId) {
        return R.ok(noticeService.queryById(noticeId));
    }

    /**
     * 新增通知信息
     */
    @SaCheckPermission("bzIns:notice:add")
    @Log(title = "通知信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NoticeBo bo) {
        return toAjax(noticeService.insertByBo(bo));
    }

    /**
     * 修改通知信息
     */
    @SaCheckPermission("bzIns:notice:edit")
    @Log(title = "通知信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NoticeBo bo) {
        return toAjax(noticeService.updateByBo(bo));
    }

    /**
     * 删除通知信息
     *
     * @param noticeIds 主键串
     */
    @SaCheckPermission("bzIns:notice:remove")
    @Log(title = "通知信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] noticeIds) {
        return toAjax(noticeService.deleteWithValidByIds(List.of(noticeIds), true));
    }
}
